import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/address_controller/address_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:get/get.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';

class AddressListPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    bool isSelectAddress = argumentsMap['isSelectAddress'];
    return Scaffold(
      backgroundColor: bgWhiteColor,
      appBar: AppBarUtil(
        title: '收货地址列表',
        backgroundColor: pageBgColor,
      ),
      body: GetBuilder<AddressController>(
        init: AddressController(),
        builder: (controller) {
          if (controller.addressArr.length <= 0) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    'assets/icons/icon_address_def.png',
                    width: 273.w,
                    height: 146.h,
                  ),
                  Text(
                    '暂无收货地址，请先添加~',
                    style: TextStyle(
                      color: textSubtitleColor,
                      fontSize: 14.sp
                    ),
                  )
                ],
              ),
            );
          } 
          return ScrollConfigurationCommon(child: ListView(
            children: controller.addressArr.map((item) {
              return InkWellUtil.shakeProofInkWell(Container(
                padding: EdgeInsets.all(16.w),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      width: .5,
                      color: lineColor
                    )
                  ),
                ),
                child: Row(
                  children: [
                    Expanded(child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(
                              '${item.consignee}',
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold
                              ),
                            ),
                            SizedBox(width: 22.w),
                            Expanded(child: Text(
                              '${item.mobile}',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 16.sp
                              ),
                            ))
                          ],
                        ),
                        SizedBox(height: 3.h),
                        Text(
                          '${item.provinceStr}${item.cityStr}${item.districtStr}${item.address}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 2,
                          style: TextStyle(
                            color: textSubtitleColor,
                            fontSize: 14.sp
                          ),
                        ),
                        SizedBox(height: 6.h),
                        Row(
                          children: [
                            Image.asset(
                              item.isDefault == 1 ? 'assets/icons/icon_radio_fill.png' : 'assets/icons/icon_radio_line.png',
                              width: 20.w,
                              height: 20.w,
                            ),
                            SizedBox(width: 6.w),
                            Text(
                              '设为默认地址',
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 14.sp
                              ),
                            )
                          ],
                        )
                      ],
                    )),
                    SizedBox(width: 12.w),
                    InkWellUtil.shakeProofInkWell(Image.asset(
                      'assets/icons/icon_edit.png',
                      width: 16.w,
                      height: 16.w,
                    ), () {
                      Get.toNamed(
                        '${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}${AppRouters.ADDRESS_LIST_PAGE}${AppRouters.ADD_EDIT_ADDRESS_PAGE}',
                        arguments: {
                          'title': '编辑地址',
                          'type': 1,
                          'data': {
                            'id': item.id,
                            'consignee': item.consignee,
                            'mobile': item.mobile,
                            'province': item.province,
                            'city': item.city,
                            'district': item.district,
                            'address': item.address,
                            'isDefault': item.isDefault,
                            'provinceStr': item.provinceStr,
                            'cityStr': item.cityStr,
                            'districtStr': item.districtStr
                          }
                        }
                      );
                    })
                  ],
                ),
              ), () {
                if (isSelectAddress) {
                  Get.back(result: item);
                }
              });
            }).toList(),
          ));
        },
      ),
      bottomNavigationBar: BottomAppBar(
        elevation: 0,
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          height: 56.h,
          alignment: Alignment.center,
          child: ButtonUtil(
            text: '新建收货人', 
            fontSize: 14.sp, 
            borderRadius: 18.w, 
            height: 36.h, 
            showAddIcon: true,
            onTop: () {
              Get.toNamed(
                '${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}${AppRouters.ADDRESS_LIST_PAGE}${AppRouters.ADD_EDIT_ADDRESS_PAGE}',
                arguments: {
                  'title': '添加地址',
                  'type': 0
                }
              );
            }
          ),
        ),
      ),
    );
  }
}